package com.property.web.controller.management;

import java.util.List;

import com.property.management.domain.CourtInfo;
import com.property.management.service.ICourtInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.property.common.annotation.Log;
import com.property.common.enums.BusinessType;
import com.property.management.domain.ResidentInfo;
import com.property.management.service.IResidentInfoService;
import com.property.common.core.controller.BaseController;
import com.property.common.core.domain.AjaxResult;
import com.property.common.utils.poi.ExcelUtil;
import com.property.common.core.page.TableDataInfo;
import org.springframework.web.servlet.ModelAndView;

/**
 * 住户信息Controller
 * 
 * @author property
 * @date 2021-05-06
 */
@Controller
@RequestMapping("/property/resident")
public class ResidentInfoController extends BaseController
{
    private String prefix = "property/resident";

    @Autowired
    private IResidentInfoService residentInfoService;
    @Autowired
    private ICourtInfoService courtInfoService;

    @RequiresPermissions("property:resident:view")
    @GetMapping()
    public String resident(ModelMap mmap)
    {
        return prefix + "/resident";
    }

    /**
     * 查询住户信息列表
     */
    @RequiresPermissions("property:resident:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ResidentInfo residentInfo)
    {
        startPage();
        List<ResidentInfo> list = residentInfoService.selectResidentInfoList(residentInfo);
        return getDataTable(list);
    }

    /**
     * 导出住户信息列表
     */
    @RequiresPermissions("property:resident:export")
    @Log(title = "住户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ResidentInfo residentInfo)
    {
        List<ResidentInfo> list = residentInfoService.selectResidentInfoList(residentInfo);
        ExcelUtil<ResidentInfo> util = new ExcelUtil<ResidentInfo>(ResidentInfo.class);
        return util.exportExcel(list, "住户信息数据");
    }

    /**
     * 新增住户信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("courtList", courtInfoService.selectCourtInfoList(new CourtInfo()));
        return prefix + "/add";
    }

    /**
     * 新增保存住户信息
     */
    @RequiresPermissions("property:resident:add")
    @Log(title = "住户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ResidentInfo residentInfo)
    {
        return toAjax(residentInfoService.insertResidentInfo(residentInfo));
    }

    /**
     * 修改住户信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ResidentInfo residentInfo = residentInfoService.selectResidentInfoById(id);
        mmap.put("residentInfo", residentInfo);
        mmap.put("courtList", courtInfoService.selectCourtInfoList(new CourtInfo()));
        return prefix + "/edit";
    }

    /**
     * 修改保存住户信息
     */
    @RequiresPermissions("property:resident:edit")
    @Log(title = "住户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ResidentInfo residentInfo)
    {
        return toAjax(residentInfoService.updateResidentInfo(residentInfo));
    }

    /**
     * 删除住户信息
     */
    @RequiresPermissions("property:resident:remove")
    @Log(title = "住户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(residentInfoService.deleteResidentInfoByIds(ids));
    }
}
